package
{
	import flash.events.MouseEvent;
	
	import mx.core.IMXMLObject;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	import mx.validators.Validator;

	public class NewRecordWindowController implements IMXMLObject
	{
		private var window:NewRecordWindow; 
		
		public function initialized(document:Object, id:String):void
		{
			this.window = document as NewRecordWindow;
			
			this.window.addEventListener(FlexEvent.CREATION_COMPLETE, this.completed);
		}
		
		private function completed(event:FlexEvent):void {
			this.window.registerButton.addEventListener(MouseEvent.CLICK, this.registerButton_click);
			this.window.cancelButton.addEventListener(MouseEvent.CLICK, this.cancelButton_click);
		}
		
		private function registerButton_click(event:MouseEvent):void {
			// バリデーション
			var validationResults:Array = Validator.validateAll(this.window.validators);
			if (validationResults.length > 0) {
				return;
			}
			
			// エラー無しなので登録処理をする
			var newRecord:Record = Record.createFrom(this.window);
			var parent:fxug1 = this.window.parentDocument as fxug1;
			parent.model.add(newRecord);
			PopUpManager.removePopUp(this.window);
		}
		
		private function cancelButton_click(event:MouseEvent):void {
			PopUpManager.removePopUp(this.window);
		}
	}
}
